package com.hyts.jgit.bean;

import com.hyts.jgit.base.spi.AuthHandler;
import lombok.Builder;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

/**
 * project-name:coffice-toolbox
 * package-name:com.hyts.gitclient.bean
 * author:Libo/Alex
 * create-date:2019-06-14 20:56
 * copyright:libo-hyts-github
 * email:libo2dev@aliyun.com
 * description:此类主要用于: Https服务认证对象模型
 */
@Slf4j
@Builder
public final class HttpsAuthModel implements AuthHandler<String[]> {


    @Getter
    private UsernamePasswordCredentialsProvider usernamePasswordCredentialsProvider;

    @Getter
    private String userName;

    @Getter
    private String password;

    /**
     * 因为抽象了传参的位置以及参数值：故此顺序必须严格要求-用户名以及密码的顺序
     * @param param
     * @return
     */
    @Override
    public boolean signIn(String[] param) {
        // 参数认证的校验工作
        if(param == null || param.length < 2){
            throw new IllegalArgumentException("用户名以及密码皆不可以为空!");
        }
        try {
            userName = param[0];
            password = param[1];
            usernamePasswordCredentialsProvider = new UsernamePasswordCredentialsProvider(userName,password);
        } catch (Exception e) {
            log.error("认证失败!",e);
            throw new IllegalArgumentException("认证失败!");
        }
        return true;
    }


    @Override
    public boolean signOut(String[] param) {
         if(usernamePasswordCredentialsProvider != null){
             usernamePasswordCredentialsProvider.clear();
             usernamePasswordCredentialsProvider = null;
         }
         return true;
    }
}
